using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace CSharpPro._5._7;

public class Demo
{
        private Dictionary<string, string> _users = new Dictionary<string, string>();
        
        public void Run()
        {
            while (true)
            {
                ShowMainMenu();
                var choice = Console.ReadLine()?.Trim();
                HandleMenuChoice(choice);
            }
        }

        private void ShowMainMenu()
        {
            Console.WriteLine("\n--- 主菜单 ---");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");
            Console.Write("请选择功能(1-3): ");
        }

        private void HandleMenuChoice(string choice)
        {
            if (!int.TryParse(choice, out int option))
            {
                Console.WriteLine("请输入有效数字");
                return;
            }

            switch (option)
            {
                case 1:
                    Login();
                    break;
                case 2:
                    Register();
                    break;
                case 3:
                    Console.WriteLine("谢谢使用，再见！");
                    Environment.Exit(0);
                    break;
                default:
                    Console.WriteLine("请输入1-3之间的数字");
                    break;
            }
        }

        private void Register()
        {
            Console.WriteLine("\n--- 用户注册 ---");
            Console.Write("请输入用户名: ");
            var username = Console.ReadLine()?.Trim();

            var (isValid, msg) = ValidateUsername(username);
            if (!isValid)
            {
                Console.WriteLine($"注册失败: {msg}");
                return;
            }

            if (_users.ContainsKey(username))
            {
                Console.WriteLine("注册失败: 用户名已存在");
                return;
            }

            Console.Write("请输入密码: ");
            var password = Console.ReadLine()?.Trim();
            (isValid, msg) = ValidatePassword(password);
            if (!isValid)
            {
                Console.WriteLine($"注册失败: {msg}");
                return;
            }

            Console.Write("请确认密码: ");
            var confirmPwd = Console.ReadLine()?.Trim();
            if (password != confirmPwd)
            {
                Console.WriteLine("注册失败: 密码与确认密码不一致");
                return;
            }
            
            _users.Add(username, password);
            Console.WriteLine("注册成功！");
        }

        private void Login()
        {
            Console.WriteLine("\n--- 用户登录 ---");
            Console.Write("请输入用户名: ");
            var username = Console.ReadLine()?.Trim();
            Console.Write("请输入密码: ");
            var password = Console.ReadLine()?.Trim();

            if (!_users.ContainsKey(username))
            {
                Console.WriteLine("登录失败: 用户名不存在");
                return;
            }

            if (_users[username] != password)
            {
                Console.WriteLine("登录失败: 密码错误");
                return;
            }

            Console.WriteLine($"登录成功！欢迎回来，{username}！");
            Console.WriteLine("--- 系统首页 ---");
            Console.WriteLine("(模拟系统首页内容)");
        }

        private (bool, string) ValidateUsername(string username)
        {
            if (string.IsNullOrWhiteSpace(username))
                return (false, "不可为空");

            if (username.Length < 3 || username.Length > 15)
                return (false, "长度必须为3-15个字符");

            if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
                return (false, "只能包含字母、数字、下划线");

            return (true, "验证通过");
        }

        private (bool, string) ValidatePassword(string password)
        {
            if (string.IsNullOrWhiteSpace(password))
                return (false, "不可为空");

            if (password.Length < 6 || password.Length > 20)
                return (false, "长度必须为6-20个字符");

            if (!Regex.IsMatch(password, @"[A-Z]"))
                return (false, "必须包含至少1个大写字母");

            if (!Regex.IsMatch(password, @"[a-z]"))
                return (false, "必须包含至少1个小写字母");

            if (!Regex.IsMatch(password, @"[0-9]"))
                return (false, "必须包含至少1个数字");

            return (true, "验证通过");
        }
    }
    